﻿using BLL;
using DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Linq;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admincp_Manufacturers : System.Web.UI.Page
{
    DigitalWorldDataContext db = new DigitalWorldDataContext();
    ManufacturerBLL s = new ManufacturerBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadData();
            rbtnStatus.Enabled = false;
            btnUpdate.Enabled = false;
        }
    }

    private void loadData()
    {
        gvManufacturer.DataSource = s.getManufacturer();
        gvManufacturer.DataBind();
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        bool status;
        if (rbtnStatus.SelectedItem.Value == "1")
        {
            status = true;
        }
        else
        {
            status = false;
        }
        manufacturer sup = new manufacturer { 
            companyname = txtName.Text,
            country =txtContry.Text,
            url=txtHomePage.Text,
            status = status
        };
        s.Manufacturer_Insert(sup);
        this.loadData();
        this.reset();
    }
    public void reset() {
        txtName.Text = String.Empty;
        txtContry.Text = String.Empty;
        txtHomePage.Text = String.Empty;
        rbtnStatus.Enabled = false;
        btnUpdate.Enabled = false;
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        bool status;
        if (rbtnStatus.SelectedItem.Value == "1")
        {
            status = true;
        }
        else
        {
            status = false;
        }
        manufacturer sup = new manufacturer {
            id =int.Parse(gvManufacturer.SelectedRow.Cells[0].Text),
            companyname = txtName.Text,
            country = txtContry.Text,
            url = txtHomePage.Text,
            status=status
        };
        s.Manufacturer_Update(sup);
        this.loadData();
        this.reset();
    }
    protected void gvSuppliers_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        int supid = int.Parse(gvManufacturer.Rows[e.NewSelectedIndex].Cells[0].Text);
        var danhsach = db.manufacturers.SingleOrDefault(s1 => s1.id == supid);
        txtName.Text = danhsach.companyname;
        txtContry.Text = danhsach.country;
        txtHomePage.Text = danhsach.url;
        rbtnStatus.Enabled = true;
        btnUpdate.Enabled = true;
    }
    protected void gvSuppliers_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvManufacturer.PageIndex = e.NewPageIndex;
        this.loadData();
    }
    protected void gvSuppliers_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int supplierid = int.Parse(gvManufacturer.Rows[e.RowIndex].Cells[0].Text);
        s.Manufacturer_Delete(supplierid);
        this.loadData();
    }
}